package climspace.hadoop;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class DownloadFromHDFS {
	private Configuration config;
	
	public DownloadFromHDFS(String host, String port) {
		config = new Configuration();
		config.set("fs.default.name", "hdfs://"+host+":"+port);
	}
	
	// if this method failed in windows with error: chmod file not found
	// add cygwin/bin to path
	public void copyRemoteToLocal(String hdfsFile, String fullLocalFilePath) {
		try {
			FileSystem fs = FileSystem.get(config);
			fs.copyToLocalFile(new Path(hdfsFile), new Path(fullLocalFilePath));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @param args "C:/data/dropbox/cmpe275/project1/sample data/mesowest.out"
	 */
	public static void main(String args[]) {
		if (args.length != 2)
			throw new RuntimeException("Incorrent number of parameter");
	
		String fileName = args[0];
		String localFile = args[1];
		
		DownloadFromHDFS u = new DownloadFromHDFS("localhost", "9000");
		u.copyRemoteToLocal(fileName, localFile);
	}
}
